package mysql

import (
	"gorm.io/gorm"
	"wms/define"
	"wms/models"
)

// GetUserBasicByUsername 根据 username 获取 user_basic，用户名唯一
func GetUserBasicByUsername(username string) *gorm.DB {
	tx := DB.Model(new(models.UserBasic)).Select("*").Where("username = ?", username)
	return tx
}

// GetUserBasicByUsernamePassword 根据 username 和 password 获取 user_basic
func GetUserBasicByUsernamePassword(username, password string) *gorm.DB {
	password = define.EncryptMd5(password)
	tx := GetUserBasicByUsername(username).Where("password = ?", password)
	return tx
}

func GetUserPasswordByIdentity(identity string) (string, error) {
	ub := new(models.UserBasic)
	err := DB.Select("password").Where("identity = ?", identity).Find(ub).Error
	return ub.Password, err
}

// GetUserBasicsByRoleIdentity 根据 role_identity 获取 user_basics，一角色对应多用户
func GetUserBasicsByRoleIdentity(roleIdentity string) *gorm.DB {
	tx := DB.Model(new(models.UserBasic)).Select("*").Where("role_identity = ?", roleIdentity)
	return tx
}

// GetUserBasicByIdentity 根据 identity 获取 user_basic
func GetUserBasicByIdentity(identity string) *gorm.DB {
	tx := DB.Model(new(models.UserBasic)).Select("*").Where("identity = ?", identity)
	return tx
}

func ChangeUserPassword(identity, password string) error {
	err := DB.Model(new(models.UserBasic)).Where("identity = ?", identity).Updates(map[string]interface{}{
		"password": define.EncryptMd5(password),
	}).Error
	return err
}

// GetUserListByKeyword 获取用户列表通过 keyword
func GetUserListByKeyword(keyword string) *gorm.DB {
	tx := DB.Model(new(models.UserBasic)).Select("user_basic.identity, user_basic.role_identity, " +
		"user_basic.username, user_basic.phone, user_basic.avatar, user_basic.created_at, user_basic.updated_at, rb.name role_name").
		Joins("LEFT JOIN role_basic rb ON rb.identity = user_basic.role_identity")
	if keyword != "" {
		tx.Where("username LIKE ?", "%"+keyword+"%")
	}
	tx.Order("sort ASC")
	return tx
}

// InsertOneUser 插入一个用户项
func InsertOneUser(ub *models.UserBasic) error {
	err := DB.Create(ub).Error
	return err
}

// UpdateUserByIdentity 更新一个用户项
func UpdateUserByIdentity(ub *models.UserBasic) error {
	err := DB.Model(new(models.UserBasic)).Where("identity = ?", ub.Identity).Updates(map[string]interface{}{
		"username":      ub.Username,
		"phone":         ub.Phone,
		"password":      ub.Password,
		"role_identity": ub.RoleIdentity,
	}).Error
	return err
}

// UpdateUserByIdentityWithoutPassword 更新一个用户项，不更新其唯一标识和密码
func UpdateUserByIdentityWithoutPassword(ub *models.UserBasic) error {
	err := DB.Model(new(models.UserBasic)).Where("identity = ?", ub.Identity).Updates(map[string]interface{}{
		"username":      ub.Username,
		"phone":         ub.Phone,
		"role_identity": ub.RoleIdentity,
	}).Error
	return err
}

// DeleteUserByIdentity 根据 identity 删除用户
func DeleteUserByIdentity(identity string) error {
	err := DB.Where("identity = ?", identity).Delete(new(models.UserBasic)).Error
	return err
}

// UpdateUserAvatarByIdentity 根据 identity 修改用户头像，并传入头像链接
func UpdateUserAvatarByIdentity(identity string, avatar string) error {
	err := DB.Model(new(models.UserBasic)).Where("identity = ?", identity).Updates(map[string]interface{}{
		"avatar": avatar,
	}).Error
	return err
}
